﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.ToolCenter.ViewModel.Services
 * 唯一标识：b8a99026-116f-4646-a7cf-00bb6c3034de
 * 文件名：LoadPluginService
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2023/12/26 13:18:45
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Reflection;
using Vampirewal.Core.Interface;
using Vampirewal.Core.Models;
using Vampirewal.ToolCenter.Plugin;

namespace Vampirewal.ToolCenter.ViewModel.Services;

public interface ILoadPluginService
{
    /// <summary>
    /// 读取插件
    /// </summary>
    void Load();

    /// <summary>
    /// 插件集合
    /// </summary>
    List<PluginInfoVO> PluginInfoVOs { get;  }

    
}

/// <summary>
///
/// </summary>
public partial class LoadPluginService : ILoadPluginService
{
    /// <summary>
    /// 构造函数
    /// </summary>
    public LoadPluginService()
    {
        PluginInfoVOs=new List<PluginInfoVO>();
    }

    public List<PluginInfoVO> PluginInfoVOs { get;private set; }

    public List<IOptions> Options { get;  set; }

    public IServiceCollection Services { get;  set; }

    public Action<bool> OnAppRun { get; set; }

    public void Load()
    {
        string PluginFolder = $"{AppDomain.CurrentDomain.BaseDirectory}Plugins";

        if (!Directory.Exists(PluginFolder))
        {
            Directory.CreateDirectory(PluginFolder);
        }
        else
        {
            var Dirs= Directory.GetDirectories(PluginFolder);

            foreach (var item in Dirs)
            {
                var files= Directory.GetFiles(item,"*.dll");
                foreach (var f in files)
                {
                    Assembly ass = Assembly.LoadFile(f);

                    #region 读取插件
                    var face = ass.GetExportedTypes().Where(w => w.GetInterface("IPlugin") != null).FirstOrDefault();

                    if (face != null)
                    {
                        var plugin = (IPlugin)Activator.CreateInstance(face)!;

                        PluginInfoVO VO = new PluginInfoVO()
                        {
                            PluginName = plugin.PluginName,
                            PluginDescription = plugin.PluginDescription,
                            PluginIconPath = $"{PluginFolder}\\{plugin.PluginFolderName}\\{plugin.PluginIconName}",
                            PluginVersion = plugin.PluginVersion,
                            ViewKey = plugin.ViewKey,
                            Height = plugin.Height,
                            Width = plugin.Width,
                        };

                        if (VO.PluginIconPath == $"{PluginFolder}\\{plugin.PluginFolderName}")
                        {
                            continue;
                        }

                        PluginInfoVOs.Add(VO);
                    }
                    else
                    {
                        continue;
                    }
                    #endregion

                    #region 读取配置

                    var startup = ass.GetExportedTypes().Where(w => w.GetInterface("IStartupBase") != null).First();
                    if (startup!=null)
                    {
                        var startupBase = (IStartupBase)Activator.CreateInstance(startup);

                        if (startupBase!=null)
                        {
                            startupBase!.RegisterConfig(Options);

                            startupBase!.RegisterService(Services);

                            OnAppRun += startupBase.OnAppRun;
                        }

                        
                    }

                    #endregion
                }
                
            }
        }

    }
}

public partial class PluginInfoVO : TopModel
{
    public PluginInfoVO()
    {
    }

    /// <summary>
    /// 插件名称
    /// </summary>
    [ObservableProperty]
    private string pluginName;

    /// <summary>
    /// 插件版本
    /// </summary>
    [ObservableProperty]
    private string pluginVersion;

    /// <summary>
    /// 插件描述
    /// </summary>
    [ObservableProperty]
    private string pluginDescription;

    /// <summary>
    /// 插件图标路径
    /// </summary>
    [ObservableProperty]
    private string pluginIconPath;

    /// <summary>
    /// 窗体Key
    /// </summary>
    [ObservableProperty]
    private string viewKey;

    /// <summary>
    /// 窗体宽度
    /// </summary>
    [ObservableProperty]
    private double width;

    /// <summary>
    /// 窗体高度
    /// </summary>
    [ObservableProperty]
    private double height;
}